package com.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.common.Common;
import com.common.ReturnCode;
import com.dao.SysOperateLogDao;
import com.dao.SysRoleDao;
import com.model.ReponseDto;
import com.model.SysMenu;
import com.model.SysOperateLog;
import com.model.SysRole;
import com.service.ISysRoleService;
import com.service.ISysUserService;

@Service
public class SysRoleServiceImpl implements ISysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private ISysUserService sysUserService;
    
    @Autowired
    private SysOperateLogDao sysOperateLogDao;

    @Override
    public ReponseDto<List<SysRole>> list() {
        List<SysRole> rlist = sysRoleDao.get(null);
        if (CollectionUtils.isEmpty(rlist)) {
            return new ReponseDto();
        }
        for (SysRole r : rlist) {
            ReponseDto<List<SysMenu>> sysMenulist = sysUserService
                    .getMenuByRoleIdAll(Integer.valueOf(String.valueOf(r.getId())));
            r.setMenulist(sysMenulist.getData());
        }
        return new ReponseDto<>(rlist);
    }

    @Override
    public ReponseDto<String> add(SysRole sysRole, String userId, String userName) {
        if (StringUtils.isEmpty(sysRole.getName()) || null == sysRole.getLevel()
                || StringUtils.isEmpty(sysRole.getStatus()) || StringUtils.isEmpty(sysRole.getRemark())
                || CollectionUtils.isEmpty(sysRole.getMenulist())) {
            return new ReponseDto<>(ReturnCode.PARAM_ERROR);
        }
        Map<String, Object> parMap = new HashMap<String, Object>();
        parMap.put("name", sysRole.getName());
        List<SysRole> slist = sysRoleDao.get(parMap);
        if (!CollectionUtils.isEmpty(slist)) {
            return new ReponseDto<>(ReturnCode.ROLE_NAME_REPEAT);
        }
        sysRoleDao.insert(sysRole);

        for (SysMenu menu : sysRole.getMenulist()) {
            Map<String, Object> mmap = new HashMap<>();
            mmap.put("menuId", menu.getId());
            mmap.put("roleId", sysRole.getId());
            sysRoleDao.inserMenu(mmap);
        }
        
        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.valueOf(userId));
        sysOperateLog.setUserName(userName);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_ADD);
        sysOperateLog.setDescription("角色管理-添加角色");
        sysOperateLog.setDataCcope("角色管理-添加角色");
        sysOperateLogDao.insert(sysOperateLog);
        
        return new ReponseDto<>();
    }

    @Override
    public ReponseDto<String> right(SysRole sysRole, String userId, String userName) {
        if (CollectionUtils.isEmpty(sysRole.getMenulist())) {
            return new ReponseDto<>(ReturnCode.PARAM_ERROR);
        }
        Map<String, Object> parMap = new HashMap<String, Object>();
        parMap.put("id", sysRole.getId());
        List<SysRole> slist = sysRoleDao.get(parMap);
        if (CollectionUtils.isEmpty(slist)) {
            return new ReponseDto<>(ReturnCode.DATA_NOT_EXISTS);
        }
        
        sysRoleDao.deleteMenu(sysRole.getId());
        
        for (SysMenu menu : sysRole.getMenulist()) {
            Map<String, Object> mmap = new HashMap<>();
            mmap.put("menuId", menu.getId());
            mmap.put("roleId", sysRole.getId());
            sysRoleDao.inserMenu(mmap);
        }
        
        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.valueOf(userId));
        sysOperateLog.setUserName(userName);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_UPDATE);
        sysOperateLog.setDescription("角色管理-设置权限");
        sysOperateLog.setDataCcope("角色管理-设置权限");
        sysOperateLogDao.insert(sysOperateLog);
        
        return new ReponseDto<>();
    }

}
